<template>
  <div>
    <!-- 6.4绑定到表单数据数据中 -->
    <el-dialog @closed="closed" :title="status.isAdd?'会员添加':'会员编辑'" :visible.sync="status.isshow">
      <el-form :model="form">
        <el-form-item label="手机号" label-width="120px">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称" label-width="120px">
          <el-input v-model="form.nickname" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="密码" label-width="120px">
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 1.静态页面
// 2.操作仓库
// 3.初始化form,并绑定数据
import { reqmemberinfo, reqmemberedit } from "../../../http/api";
import { successAlert } from "../../../utils/alert";
// 引入辅助函数
import { mapGetters, mapActions } from "vuex";
export default {
  props: ["status"],
  data() {
    return {
      formLabelWidth: "120px",
      form: {
        uid: "",
        nickname: "",
        phone: "",
        password: "",
        status: 1
      }
    };
  },
  methods: {
    ...mapActions({
      reqList: "vip/reqList"
    }),

    // 清空form
    empty() {
      this.form = {
        uid: "",
        nickname: "",
        phone: "",
        password: "",
        status: 1
      };
    },
// ==============================两种请求的方法=======================
    //7 父元素点击了编辑按钮，触发子组件的的getOne方法
    async getOne(uid) {
      //发请求---->获取一条数据
      let res = await reqmemberinfo({
        uid: uid
      });
      // 赋值
      this.form = res.data.list;
      this.form.password = "";
    },
    //点击了修改
    update() {
      if (this.form.nickname == "") {
        errorAlert("昵称不能为空");
        return;
      }
      if (this.form.phone == "") {
        errorAlert("电话不能为空");
        return;
      }
      // 发请求
      reqmemberedit(this.form).then(res => {
        if (res.data.code == 200) {
          // 1.弹成功
          successAlert(res.data.msg);

          // 2.清空form
          this.empty();
          //3. 弹框消失
          this.status.isshow = false;
          //4.列表重新渲染
          this.reqList();
        }
      });
    },
    cancel() {
      // 通知父组件
      this.status.isshow = false;
    },
    // 9.修改Bug
    // 我们希望点击了添加之后，取消，在点击添加，数据还在，但点击了编辑之后，取消后点击添加，清空内容
    // 给弹框添加close关闭事件---如果是添加的取消，什么都不做；如果是编辑的取消，form清空
    closed() {
      if (!this.status.isAdd) {
        this.empty();
      }
    }
  },
};
</script>

<style>
</style>